package model;

import util.DBUtil;

import java.util.List;
import java.util.Map;

public class UserService {

    private DBUtil db = null;

    public UserService() {
        db = new DBUtil();
    }


    /**
     * 判断用户名是否可用
     *
     * @param username
     * @return true表示可用，false表示用户名已被使用
     */
    public boolean check(String username) {
        boolean f = false;
        String sql = "select * from user where username=?";
        Map<String, String> u = db.getMap(sql, new String[]{username});
        if (u != null) {
            f = true;
        }
        System.out.println("check检查了");
        return f;
    }

    /**
     * 注册用户
     *
     * @param un
     * @param pw
     * @param tel
     * @param addr
     * @return int 影响的行数
     */
    public int register(String un, String pw, String tel, String addr) {
        String sql = "insert into user values(null,?,?,0,?,?)";
        String[] params = {un, pw, tel, addr};
        System.out.println("经过注册了");
        return db.update(sql, params);
    }

    public int updateUser(String pw, String tel, String addr, String id) {
        String sql = "update user set password=?,telephone=?,address=? where id=?";
        String[] params = {pw, tel, addr, id};
        return db.update(sql, params);
    }

    public int updateUser(Map<String, String> m) {
        String sql = "update user set username=? ,password=?,telephone=?,address=? where id=?";
        String[] params = {m.get("username"), m.get("password"), m.get("telephone"), m.get("address"), m.get("id")};
        //输出params数组
//        for (String s : params)
//            System.out.println(s);


        return db.update(sql, params);
    }

    /**
     * 根据id查找用户
     *
     * @param id
     * @return 封装了一个用户信息的map对象
     */
    public Map<String, String> getUserById(String id) {
        String sql = "select * from user where id= ? ;";
        String[] params = {id};
        return db.getMap(sql, params);

    }

    //根据用户名模糊查找所以用户;
    public List<Map<String, String>> getUsers(String username) {
        String sql = "select * from user";
        String[] params = null;
        if (username != null && !"".equals(username)) {
            sql = sql + " where username like ?";
            params = new String[]{
                    "%" + username + "%"
            };

        }
        return db.getList(sql, params);

    }

    //    public int delUser(int id) {
//        String sql = "delete from user where id=" + id;
//        return db.update(sql);
//    }
    //删除菜品
    public int delUser(String id) {
        String sql = "delete from user where id=?";
        return db.update(sql, new String[]{id});
    }

//    public static void main(String[] args) {
//        UserService userService = new UserService();
//        List<Map<String, String>> list = userService.getUsers(null);
//        for (Map<String, String> u : list)
//            System.out.print(u.get("username"));
//
//    }


    //    login
    public Map<String, String> login(String username, String password) {
        String sql = "select * from user where username = ? and password = ?;";
        return db.getMap(sql, new String[]{username, password});
    }

    /**
     * 根据用户名查找用户id
     *
     * @param username
     * @return 测试成功 返回用户id
     */
    public Map<String, String> getIdByUsername(String username) {
        String sql = "select id from user where username= ? ;";
        String[] params = {username};
        return db.getMap(sql, params);

    }

    //测试getIdByUsername()
//    public static void main(String[] args) {
//        UserService userService = new UserService();
//        Map<String, String> map = userService.getIdByUsername("sy");
//        System.out.println(map.get("id"));
//    }

    //测试login()

//    public static void main(String[] args) {
//        UserService userService = new UserService();
//        Map<String, String> map = userService.login("admin", "123");
//        System.out.println(map);
//    }

}
